package com.hwd.srb.core.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hwd.srb.common.exception.Assert;
import com.hwd.srb.common.result.R;
import com.hwd.srb.common.result.ResponseEnum;
import com.hwd.srb.common.constant.IntegralConstants;
import com.hwd.srb.core.pojo.entity.IntegralGrade;
import com.hwd.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author 黄伟东/Victor
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
@ApiModel("后台管理积分等级接口")
@Api(tags = "积分等级管理")
@Slf4j
//@CrossOrigin
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    /**
     * 查询所有积分等级
     */
    @GetMapping("/list")
    @ApiOperation("积分等级列表")
    public R list() {

        List<IntegralGrade> integralGradeList =
                integralGradeService.list(Wrappers.lambdaQuery(IntegralGrade.class).orderByAsc(IntegralGrade::getIntegralStart));

        Assert.notNull(integralGradeList, IntegralConstants.LIST_ALL_ERROR);

        return R.ok()
                .message(IntegralConstants.LIST_ALL_SUCCESS)
                .data(IntegralConstants.INTEGRAL_GRADE_LIST, integralGradeList);
    }

    /**
     * 根据id删除积分等级
     *
     * @param id 删除的主键
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除积分等级", notes = "逻辑删除")
    public R removeById(
            @ApiParam(value = "删除的主键", required = true, example = "1")
            @PathVariable Long id) {

        boolean result = integralGradeService.removeById(id);
        Assert.isTrue(result, IntegralConstants.REMOVE_BY_ID_ERROR);

        return R.ok().message(IntegralConstants.REMOVE_BY_ID_SUCCESS);
    }


    /**
     * 新增积分等级
     *
     * @param integralGrade 积分等级对象
     */
    @PostMapping("/save")
    @ApiOperation("新增积分等级")
    public R save(@ApiParam(value = "积分等级对象", required = true) @RequestBody IntegralGrade integralGrade) {

        // 如果借款额度为空就手动抛出一个自定义的异常！
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean result = integralGradeService.save(integralGrade);
        Assert.isTrue(result, IntegralConstants.SAVE_ERROR);

        return R.ok().message(IntegralConstants.SAVE_SUCCESS);
    }


    /**
     * 根据id获取积分等级
     *
     * @param id 查询的主键
     */
    @GetMapping("/get/{id}")
    @ApiOperation("根据id获取积分等级")
    public R getById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable Long id) {

        IntegralGrade integralGrade = integralGradeService.getById(id);

        // 断言积分等级存在，否者报自定义异常
        Assert.notNull(integralGrade, IntegralConstants.NOT_EXIST_ERROR);

        return R.ok()
                .message(IntegralConstants.GET_BY_ID_SUCCESS)
                .data(IntegralConstants.INTEGRAL_GRADE, integralGrade);
    }

    /**
     * 更新积分等级
     *
     * @param integralGrade 积分等级对象
     */
    @PutMapping("/update")
    @ApiOperation("更新积分等级")
    public R updateById(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade) {

        Assert.notNull(integralGrade.getId(), ResponseEnum.ERROR);

        boolean result = integralGradeService.updateById(integralGrade);

        // 断言更新成功，否者报自定义异常
        Assert.isTrue(result, IntegralConstants.UPDATE_BY_ID_ERROR);

        return R.ok().message(IntegralConstants.UPDATE_BY_ID_SUCCESS);
    }


}

